﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using WorkSpace.UI.WorkSpaceRef;
using System.Windows.Media.Imaging;
using System.Windows.Browser;

namespace WorkSpace.UI.Controls
{
    public partial class LinkPart : UserControl
    {
        private Link _link;
        private string _account;

        public LinkPart(Link link, string account)
        {
            InitializeComponent();

            _ctlGridTask.Visibility = System.Windows.Visibility.Collapsed;


            _link = link;
            _account = account;

            _ctlImage.Source = new BitmapImage(new Uri(_link.IconUrl, UriKind.RelativeOrAbsolute));
            _ctlText.Text = _link.Caption;

            this.Loaded += new RoutedEventHandler(LinkPart_Loaded);
        }

        void LinkPart_Loaded(object sender, RoutedEventArgs e)
        {
            WorkSpaceRef.WorkSpaceWebServiceSoapClient client = WorkSpaceModule.CreateWorkSpaceWebServiceProxy();
            client.GetTaskCountCompleted += new EventHandler<GetTaskCountCompletedEventArgs>(client_GetTaskCountCompleted);
            client.GetTaskCountAsync(_link.Id, _account);
        }

        void client_GetTaskCountCompleted(object sender, GetTaskCountCompletedEventArgs e)
        {
            if (e.Error == null && e.Result.IsSuccess)
            {
                if (e.Result.Data > 0)
                {
                    _ctlGridTask.Visibility = System.Windows.Visibility.Visible;
                    if (e.Result.Data > 99)
                    {
                        _ctlTxtTaskCount.Text = "99";
                    }
                    else
                    {
                        _ctlTxtTaskCount.Text = e.Result.Data.ToString();
                    }
                    return;
                }
            }
            _ctlGridTask.Visibility = System.Windows.Visibility.Collapsed;
            _ctlTxtTaskCount.Text = string.Empty;
        }

        private void _ctlBtn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Uri url = new Uri(_link.LinkUrl, UriKind.RelativeOrAbsolute);
                HtmlPopupWindowOptions options = new HtmlPopupWindowOptions();
                options.Resizeable = true;
                HtmlPage.PopupWindow(url, "", options);
            }
            catch
            {
                MessageBox.Show("不是有效的地址!");
            }
        }
    }
}
